package com.example.program.demos.service;

import com.example.program.demos.bean.TotalAttendance;
import com.example.program.demos.mapper.ReportMapper;
import com.example.program.demos.mapper.TianXingMapper;
import com.sun.corba.se.spi.ior.ObjectKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.time.YearMonth;

@Service
public class TianXingService {
    @Autowired
    TianXingMapper tianXingMapper;

    @Autowired
    ReportMapper reportMapper;

    public List<Map<String, Object>> selectAll(Map<String, Object> map) {
        return tianXingMapper.selectAll(map);
    }

    public void insertAttendance(Map<String, Object> totalAttendance) {
        tianXingMapper.insertAttendance(totalAttendance);
    }

    public void insertUser(Map<String, Object> map, Map<String, Object> userInf) throws ParseException {
        Map<String, Object> user = new HashMap<>();
        Map<String, Object> attendance = new HashMap<>();
        String name = map.get("name").toString();
        String password = map.get("password").toString();
        String bankCard = map.get("bankCard").toString();
        int workPrice = map.get("workPrice").toString().isEmpty() ? 0 : Integer.parseInt(map.get("workPrice").toString());
        double totalAttendance = map.get("attendance").toString().isEmpty() ? 0.0: Double.parseDouble(map.get("attendance").toString());
        Date month = new SimpleDateFormat("yyyy-MM").parse(userInf.get("month").toString());
        int companyId = Integer.parseInt(userInf.get("company").toString());
        int programId = Integer.parseInt(userInf.get("programm").toString());
        String  group = userInf.get("group").toString();
        double salary = map.get("salary").toString().isEmpty() ? 0.00 : Double.parseDouble(map.get("salary").toString());
        user.put("name", name);
        user.put("password", password);
        user.put("bankCard", bankCard);
        user.put("companyId", companyId);
        user.put("programId", programId);
        user.put("workPrice", workPrice);
        // 查看考勤汇总中是否存在该人
        Map<String, Object> mapp = new HashMap<>();
        mapp.put("month", month);
        mapp.put("programId", programId);
        mapp.put("password", password);
        Map<String, Object> list = tianXingMapper.reportAttendance(mapp);
        if(list == null || list.isEmpty()) {
            int days = getDaysInMonth(userInf.get("month").toString());
            Random random = new Random();
            int[] dailyAttendance = new int[31];
            int count = 0;
            while (count < totalAttendance) {
                int randomDay = random.nextInt(days);
                if(dailyAttendance[randomDay] == 0) {
                    dailyAttendance[randomDay] = 1;
                    count++;
                }
            }
            attendance.put("_1",dailyAttendance[0]);
            attendance.put("_2",dailyAttendance[1]);
            attendance.put("_3",dailyAttendance[2]);
            attendance.put("_4",dailyAttendance[3]);
            attendance.put("_5",dailyAttendance[4]);
            attendance.put("_6",dailyAttendance[5]);
            attendance.put("_7",dailyAttendance[6]);
            attendance.put("_8",dailyAttendance[7]);
            attendance.put("_9",dailyAttendance[8]);
            attendance.put("_10",dailyAttendance[9]);
            attendance.put("_11",dailyAttendance[10]);
            attendance.put("_12",dailyAttendance[11]);
            attendance.put("_13",dailyAttendance[12]);
            attendance.put("_14",dailyAttendance[13]);
            attendance.put("_15",dailyAttendance[14]);
            attendance.put("_16",dailyAttendance[15]);
            attendance.put("_17",dailyAttendance[16]);
            attendance.put("_18",dailyAttendance[17]);
            attendance.put("_19",dailyAttendance[18]);
            attendance.put("_20",dailyAttendance[19]);
            attendance.put("_21",dailyAttendance[20]);
            attendance.put("_22",dailyAttendance[21]);
            attendance.put("_23",dailyAttendance[22]);
            attendance.put("_24",dailyAttendance[23]);
            attendance.put("_25",dailyAttendance[24]);
            attendance.put("_26",dailyAttendance[25]);
            attendance.put("_27",dailyAttendance[26]);
            attendance.put("_28",dailyAttendance[27]);
            attendance.put("_29",dailyAttendance[28]);
            attendance.put("_30",dailyAttendance[29]);
            attendance.put("_31",dailyAttendance[30]);
            attendance.put("name", name);
            attendance.put("password", password);
            attendance.put("month", month);
            attendance.put("group", group);
            attendance.put("companyId", companyId);
            attendance.put("programId", programId);
            attendance.put("attendance", totalAttendance);
            attendance.put("salary", salary);
            tianXingMapper.insertAttendance(attendance);
        } else {
            list.put("salary", salary);
            tianXingMapper.updateAttendance(list);
        }
        Map<String, Object> userMap = tianXingMapper.selectUserInf(user);
        if(userMap == null || userMap.isEmpty()) {
            tianXingMapper.insertUser(user);
        } else {
            user.put("id", userMap.get("id"));
            tianXingMapper.updateUser(user);
        }

    }
    public int selectAllCounts(Map<String, Object> map) {
        return tianXingMapper.selectAllCounts(map);
    }

    public int getDaysInMonth(String yearMonth) {
        String[] parts = yearMonth.split("-");
        int year = Integer.parseInt(parts[0]);
        int month = Integer.parseInt(parts[1]);
        return YearMonth.of(year, month).lengthOfMonth();
    }


    public void updateUser(Map<String, Object> userInf) {
        tianXingMapper.updateUserInf(userInf);
    }

    public void updateUserAttendance(Map<String, Object> userInf) {
        tianXingMapper.updateUserAttendance(userInf);
    }
}
